﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Text;
using Baosight.ZNH.ZMCT.UI.Utils;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon.Validators;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.ZNH.ZMCT.UI.PopWindows;


namespace Baosight.ZNH.ZMCT.UI
{
    public partial class ZMCT02 : EFViewCmes
    {
        private DataInterfaceBlock block = new DataInterfaceBlock();//构造前台下拉列表
        private string mode;
        public ZMCT02()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(block.GetDeviceBlock(false));//短信发送设备状态
            initData.AddBlock(block.GeStartOrStopBlock(false));//是否启动服务
            mode = initData.Get("mode").ToString();
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// Load事件中初始化父类EFViewCmesProperty的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.ParameterSetSP.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 确定按钮操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnConfirmButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            //ValidatorManager自定义的校验方法
            bool result = true;
            ValidatorManager group = this.GetChildObject<ValidatorManager>(null);
            if (group != null)
            {
                result = group.IsValidSucces;
            }

            bool _validateField = ThisValidateField() & this.ValidateField() & result;//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup();//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
            }
            else
            {
                if (!this.fdPersonSql.Value.ToString().Contains("no"))
                {
                    ValidateMessagePopup("sql中必须包含no(人员工号)的别名字段！");
                    this.TempButtonOpen(true);
                    return;
                }
                if (!this.fdPersonSql.Value.ToString().Contains("name"))
                {
                    ValidateMessagePopup("sql中必须包含name(人员姓名)的别名字段！");
                    this.TempButtonOpen(true);
                    return;
                }
                if (!this.fdPersonSql.Value.ToString().Contains("mobileNum"))
                {
                    ValidateMessagePopup("sql中必须包含mobileNum(手机号码)的别名字段！");
                    this.TempButtonOpen(true);
                    return;
                }
                if (!this.fdPersonSql.Value.ToString().Contains("departmentId"))
                {
                    ValidateMessagePopup("sql中必须包含departmentId(部门ID)的别名字段！");
                    this.TempButtonOpen(true);
                    return;
                }
                if (!this.fdDepartmentSql.Value.ToString().Contains("keyID"))
                {
                    ValidateMessagePopup("sql中必须包含keyID(主键)的别名字段！");
                    this.TempButtonOpen(true);
                    return;
                }
                if (!this.fdDepartmentSql.Value.ToString().Contains("department"))
                {
                    ValidateMessagePopup("sql中必须包含department(部门名称)的别名字段！");
                    this.TempButtonOpen(true);
                    return;
                }
                base.Submit(this.BuildSubmitEIInfo("ZMCT02", mode, "result"), DoCommitCompleted);
            }
        }

        /// <summary>
        /// 点击"确定"后的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.GetStatus() == -1)
            {
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                if (mode.Equals("insert"))
                {
                    base.OpenEFWindowAlert("新增成功", true);
                }
                else
                {
                    base.OpenEFWindowAlert("修改成功", true);
                }
            }
            this.TempButtonOpen(true);
        }

        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }

        private void ShowPersonClick(object sender, RoutedEventArgs e)
        {
            EIInfo info = new EIInfo();
            info.Set("service", "ZMCT09");
            info.Set("method", "showQuery");
            info.Set("fdSql", this.fdPersonSql.Value);
            info.Set("fdDs", this.fdPersonDs.Value);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(info, ShowPersonCompleted);
        }

        private void ShowPersonCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo info = e.EIInfo;
            if (info.GetStatus() != -1)
            {
                ShowQueryValues showQueryValues = new ShowQueryValues();//构造一个新的页面
                showQueryValues.EIDataContext = new Platform.Core.UI.EIDataContext();
                showQueryValues.EIDataContext.Update(info);

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner;
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = "查看";//设置Title
                _popup.Content = showQueryValues;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);

                _popup.ShowDialog();//模态显示弹出窗口
            }
            else
            {
                ValidateMessagePopup(info.GetMsg());
            }
        }

        private void ShowDepartmentClick(object sender, RoutedEventArgs e)
        {
            EIInfo info = new EIInfo();
            info.Set("service", "ZMCT09");
            info.Set("method", "showQuery");
            info.Set("fdSql", this.fdDepartmentSql.Value);
            info.Set("fdDs", this.fdDepartmentDs.Value);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(info, ShowDepartmentCompleted);
        }

        private void ShowDepartmentCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo info = e.EIInfo;
            if (info.GetStatus() != -1)
            {
                ShowQueryValues showQueryValues = new ShowQueryValues();//构造一个新的页面
                showQueryValues.EIDataContext = new Platform.Core.UI.EIDataContext();
                showQueryValues.EIDataContext.Update(info);

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner;
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = "查看";//设置Title
                _popup.Content = showQueryValues;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);

                _popup.ShowDialog();//模态显示弹出窗口
            }
            else
            {
                ValidateMessagePopup(info.GetMsg());
            }
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {

            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
    }
}
